<?php
/**
 * User: lsx
 * Date: 2017/6/24
 * Time: 11:42
 * Progress:0%
 */

namespace logic\admin\index;
use model\AccountRechargeOrder;
use think\db;


class LAccountRechargeOrder
{

    public function index_data($param){
        $model = new AccountRechargeOrder();
        $keywords=$param['search'];
        $where=array();
        if (!empty($keywords)) {
            $where['aro.order_sn|a.nick|ap.remark|a.mobile|a.wechat'] = array('like', "%{$keywords}%"); //查询条件
        }

        // <editor-fold defaultstate="collapsed" desc="开始时间 截止时间">
        if (!empty($param['begindate'])) {
            $begin_date = strtotime($param['begindate']);
        } else {
            $begin_date = '';
        }
        if (!empty($param['enddate'])) {
            $end_date = strtotime('+1 day', strtotime($param['enddate']));
        } else {
            $end_date = '';
        }

        if (!empty($begin_date) && !empty($end_date)) {
            $where['aro.order_time'] = array('between', "{$begin_date},{$end_date}");
        } else if (!empty($begin_date) && empty($end_date)) {
            $where['aro.order_time'] = array('egt', $begin_date);
        } else if (empty($begin_date) && !empty($end_date)) {
            $where['aro.order_time'] = array('elt', $end_date);
        }
        // </editor-fold>

        $total=$model->alias('aro')
            ->join('account a','a.id=aro.account_id','left')
            ->join('account_payment ap','ap.id=aro.payment_id','left')->where($where)->count('aro.id'); //记录总数
        $page_size =$param['limit']; //获取分页数
        $start = $param['offset']; //获取开始的记录数
        //获取排序字段------------------------------------------------------------开始

        $sort_name = $param['sort'];
        $sort_desc = $param['order'];

        if (!empty($sort_name)) {
            $sort[$sort_name] = $sort_desc;
        } else {
            $sort['aro.order_time'] = 'desc'; //默认排序
        }
        //获取排序字段------------------------------------------------------------结束

        $rows = $model->alias('aro')
            ->join('account a','a.id=aro.account_id','left')
            ->join('account_payment ap','ap.id=aro.payment_id','left')
            ->field('aro.*,a.nick,a.mobile,a.wechat,ap.pay_type,ap.pay_time,ap.remark')
            ->where($where)
            ->limit($start, $page_size)
            ->order($sort)
            ->select();

        $array=array(
            'total'=>$total,
            'rows'=>$rows
        );

        return $array;

    }
}